export function fire(pos:GameVector3,dir:GameVector3,speed:number,time:number)
{
    time = Math.min(time,10*1000)

    for(const e of Bullets)
    {
        if(!e.use)
        {
            e.fire(pos,dir,speed,time)
            return
        }
    }
    new Bullet(pos,dir,speed,time)
} 

class Bullet {

    use:boolean = true
    entity:GameEntity

    constructor(pos:GameVector3,dir:GameVector3,speed:number,time:number)
    {
        console.log('new bullet')
        this.entity = world.createEntity({
            mesh:'mesh/单位像素.vb',
            meshScale:new GameVector3(0.2,0.2,0.2),
            meshColor:new GameRGBAColor(1,1,0,1),
            
            position:pos,
            velocity:dir.scale(speed),

            gravity:false,
            collides:false,
            fixed:false
        })!

        Bullets.push(this)

        setTimeout(() => {
            this.entity.position.y = -10
            this.entity.velocity.set(0,0,0)
            this.use = false
        },time)
    }

    fire(pos:GameVector3,dir:GameVector3,speed:number,time:number)
    {
        this.use = true

        this.entity.position = pos
        this.entity.velocity = dir.scale(speed)

        setTimeout(() => {
            this.entity.position.y = -10
            this.entity.velocity.set(0,0,0)
            this.use = false
        },time)
    }
}

const Bullets:Bullet[] = []